package com.self.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.self.commonutils.JwtUtils;
import com.self.educenter.entity.UcenterMember;
import com.self.educenter.entity.vo.RegisterVo;
import com.self.educenter.mapper.UcenterMemberMapper;
import com.self.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.commonutils.MD5;
import com.self.servicebase.handler.MyException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.lang.reflect.Member;
import java.util.Date;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author xiaozeng
 * @since 2020-10-04
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //登录
    @Override
    public String login(UcenterMember member) {

        //获取登录的手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"登录失败~~");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        //判断查询对象是否为空
        if(mobileMember == null){//表明没有这个手机号
            throw new MyException(20001,"登录失败~~");
        }

        //判断手机号查询的对象，密码是否正确
        if (!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new MyException(20001,"登录失败~~");
        }

        //判断用户是否已被禁用 is_disabled
        if(mobileMember.getIsDisabled()){
        throw new MyException(20001,"登录失败~~");
        }

        //登录成功，使用JWT生成token并返回
        String token = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());

        return token;
    }

    //注册
    @Override
    public void register(RegisterVo registerVo) {

        //获取注册的数据
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String nickname = registerVo.getNickname();//昵称
        String password = registerVo.getPassword();//密码

        //进行非空判断,任何一个值为空都抛出异常
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new MyException(20001,"注册失败~~");
        }

        //判断手机验证码是否正确
        //获取Redis中的验证码，与输入的验证码比较
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(redisCode)){
            throw new MyException(20001,"注册失败~~");
        }

        //判断手机号不能重复
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new MyException(20001,"注册失败");
        }

        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setPassword(MD5.encrypt(password));//密码需要加密再存入数据库
        member.setIsDisabled(false);
        member.setAvatar("https://edu8002.oss-cn-shanghai.aliyuncs.com/teacher_image/1656.jpg");
        member.setGmtCreate(new Date());
        member.setGmtModified(new Date());
        baseMapper.insert(member);

    }

    //根据openid查询数据库中是否有对应的用户信息
    @Override
    public UcenterMember getMemberByOpenId(String openid) {

        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid", openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天的注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }

}
